﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cisoft.Booking.BLL;
using Cisoft.Booking.Model;

public partial class Business_Booking_SetEdit : WebUI.Page.BasePage
{
    private DishBO dishBO = new DishBO();
    private DishMO dishMO = new DishMO();
    private SetBO setBO = new SetBO();
    private SetMO setMO = new SetMO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //绑定菜式
            BindSetDish();
            //绑定选择菜式
            FillModel();
        }
    }

    #region 按钮事件
    /// <summary>
    /// 修改事件
    /// </summary>
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        int i = 0;
        string strError = "";
        setMO = GetModel();
        try
        {
            i = setBO.Update(setMO);
        }
        catch (Exception ex)
        {
            strError = ex.Message;
        }
        if (i > 0)
        {
            PageRight("操作成功！", "SetManagement.aspx", "返回管理", "");
        }
        else
        {
            PageError(strError, Request.RawUrl);
        }
    }
    /// <summary>
    /// 返回事件
    /// </summary>
    protected void butrReset_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("SetManagement.aspx");
    }
    #endregion

    #region 填充规则实体类 private RuleMO GetModel()
    /// <summary>
    /// 填充规则实体类 
    /// </summary>
    private SetMO GetModel()
    {
        string setname = tb_SetName.Text.Trim();
        string setcode = tb_SetCode.Text.Trim();
        string setprice = tb_SetPrice.Text.Trim();
        string setpic = GetImagePath();
        string setremark = tb_SetRemark.Text.Trim() == "" ? "无" : tb_SetRemark.Text;

        setMO = setBO.GetModel(GetSetID());
        //setMO.SetID = GetSetID();
        if(setBO.UpdateIsExsistSetName(setMO.SetName,setname))
        {
            PageError("已经存在相同的名称！",Request.RawUrl);
        }
        else
	    {
            setMO.SetName = setname;
	    }
        if(setBO.UpdateIsExsistSetCode(setMO.SetCode,setcode))
        {
            PageError("已经存在相同的代码！",Request.RawUrl);
        }
        else
	    {
            setMO.SetCode = setcode;
	    }     
        setMO.SetPic = setpic;
        setMO.SetPrice = Convert.ToDecimal(setprice);
        setMO.SetDish = GetDishIDs();
        setMO.SetRemark = setremark;
        

        return setMO;
    }
    #endregion

    #region 获得上传控图片路径
    /// <summary>
    /// 获得上传控图片路径
    /// </summary>
    private string GetImagePath()
    {
        string path = "";
        //if (ViewState["ImagePath"] != null)
        //{
        //    path = ViewState["ImagePath"].ToString();
        //}

        return path;
    }
    #endregion

    #region 绑定菜式
    /// <summary>
    /// 绑定菜式
    /// </summary>
    private void BindSetDish()
    {
        DataSet ds = dishBO.GetAllList();
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                ListItem li = new ListItem();
                li.Text = dr["DishName"].ToString() + "     " +
                    dr["DishPrice"].ToString().Split('.')[0] + "." + dr["DishPrice"].ToString().Split('.')[1].Substring(0, 2) + "元";
                li.Value = dr["DishID"].ToString();

                lb_SetDish.Items.Add(li);
            }
        }
    }
    #endregion

    #region 图片上传事件
    protected void lbtn_UploadImage_Click(object sender, EventArgs e)
    {
        //if (!string.IsNullOrEmpty(InputFile1.FileName))
        //{
        //    if (InputFile1.HasFile)
        //    {
        //        //文件名
        //        string filename = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(InputFile1.FileName);
        //        //保存文件路径
        //        string filefullname = System.IO.Path.Combine(Request.PhysicalApplicationPath + "\\Upload", filename);
        //        //上传文件
        //        InputFile1.MoveTo(filefullname, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);
        //        //显示讯息
        //        double size = Math.Round(InputFile1.ContentLength / 1024F, 2);
        //        //Label1.Text = string.Format("来源:{0} ({1}kb)<br/>上传文件名称：{2}。", InputFile1.FileName, size, filename);

        //        //image1.Src = Server.MapPath(filefullname.Replace(@"\\",@"\"));
        //        Image1.ImageUrl = "../../../Upload/" + filename;

        //        ViewState["ImagePath"] = Image1.ImageUrl;
        //    }
        //}
    }
    #endregion

    #region 选择菜式事件
    protected void lb_SetDish_SelectedIndexChanged(object sender, EventArgs e)
    {
        double AllPrice = 0.0;
        foreach (ListItem li in lb_SetDish.Items)
        {
            if (li.Selected == true)
            {
                dishMO = dishBO.GetModel(int.Parse(li.Value));
                AllPrice += Convert.ToDouble(dishMO.DishPrice);
            }
        }

        lbl_AllCount.Text = "合计：" + AllPrice.ToString() + "元";
    }
    #endregion

    #region 获取菜式ID string GetDishIDs()
    private string GetDishIDs()
    {
        string strIDs = "";
        foreach (ListItem li in lb_SetDish.Items)
        {
            if (li.Selected == true)
            {
                strIDs += li.Value + ",";
            }
        }
        return strIDs;
    }
    #endregion

    #region 获得套餐标识 int GetSetID()
    /// <summary>
    /// 获得套餐标识
    /// </summary>
    private int GetSetID()
    {
        int id = 0;
        if (Request.QueryString["SetID"] != null)
        {
            id = int.Parse(Request.QueryString["SetID"].ToString());
        }

        return id;
    }
    #endregion

    #region 填充页面表格 void FillModel()
    /// <summary>
    /// 填充页面表格
    /// </summary>
    private void FillModel()
    {
        int id = GetSetID();
        if (id > 0)
        {
            setMO = setBO.GetModel(id);

            tb_SetName.Text = setMO.SetName;
            tb_SetCode.Text = setMO.SetCode;
            //if (!string.IsNullOrEmpty(setMO.SetPic))
            //{
            //    Image1.ImageUrl = setMO.SetPic;
            //}
            string[] strs = setMO.SetDish.Remove(setMO.SetDish.Length-1,1).Split(',');
            //填充套餐菜式
            FillSetDish(strs);
            //填充套餐价格
            tb_SetPrice.Text = setMO.SetPrice.ToString().Split('.')[0] + "." + setMO.SetPrice.ToString().Split('.')[1].Substring(0, 2);
            tb_SetRemark.Text = setMO.SetRemark;
        }
        else
        {
            PageError("不存在该套餐！", "SetManagement.aspx");
        }
    }
    #endregion

    #region 填充套餐菜式
    private void FillSetDish(string[] strInput)
    {
        double AllPrice = 0.0;
        foreach (string s in strInput)
        {
            foreach (ListItem li in lb_SetDish.Items)
            {
                if (li.Value == s)
                {
                    li.Selected = true;
                    dishMO = dishBO.GetModel(int.Parse(li.Value));
                    AllPrice += Convert.ToDouble(dishMO.DishPrice);
                }
            }
        }
        //显示实际价格
        lbl_AllCount.Text = "合计：" + AllPrice.ToString() + "元";

    }
    #endregion
}
